package demo1.Frames;

import af.swing.layout.HLayout;
import af.swing.layout.VLayout;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TrySet extends JFrame {
    JPanel root = new JPanel();
    JLabel label = new JLabel("一缕和谐的光芒");
    JTextField textField = new JTextField(20);
    JCheckBox checkBox = new JCheckBox("同意协议");
    JButton nextButton = new JButton("下一步");
    JComboBox<String> comboBox = new JComboBox<>();

    public TrySet(String title) {
        super(title);
        this.setContentPane(root);
        root.setLayout(new VLayout(10));
        InitFrame();
    }

    public void InitFrame() {
        InitLabel();
        InitTextField();
        InitCheckBox();
        InitComboBox();
    }

    public void InitLabel() {
        root.add(label);

        label.setFont(new Font("宋体", Font.BOLD,20));
        label.setForeground(new Color(128, 0, 255, 255));
        label.setOpaque(true);//设置背景不透明
        label.setBackground(new Color(243, 255, 79));
        label.setPreferredSize(new Dimension(250,30));
        label.setHorizontalAlignment(SwingConstants.CENTER);
    }

    public void InitTextField() {
        root.add(textField);
        textField.setText("您!");

        JButton button = new JButton("OK");
        root.add(button);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String str = textField.getText();
                System.out.println(str);
            }
        });
    }

    public void InitCheckBox() {
        root.add(checkBox);
        root.add(nextButton);

        checkBox.setSelected(false);
        nextButton.setEnabled(false);

        checkBox.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(checkBox.isSelected()) {
                    nextButton.setEnabled(true);
                }else {
                    nextButton.setEnabled(false);
                }
            }
        });
    }

    public void InitComboBox() {
        root.add(comboBox);

        comboBox.addItem("past");
        comboBox.addItem("present");
        comboBox.addItem("future");
        comboBox.addItem("beyond");

        comboBox.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println(comboBox.getSelectedItem());
            }
        });
    }
}
